/*
 * Copyright (c) 2023-2025 110476592@qq.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @file screenshot_panel.h
 * @brief 截图工具 
 * @author 110476592@qq.com 
 * @note   承接Qt项目外包定制开发。长期接收C++项目实训学徒,联系QQ：110476592。
 */

 
#ifndef SCREENSHOT_PANEL_H
#define SCREENSHOT_PANEL_H

#include <QWidget>
#include <QPixmap>
#include <QRect>
#include <QPoint>
#include <QMouseEvent>
#include <QRubberBand>
#include <QVector>
#include <drawboard/draw_panel.h>

/**
 * @brief  ScreenshotPanel
 */

class ScreenshotPanel : public DrawPanel
{
    Q_OBJECT

public:
    ScreenshotPanel(const QRect & rect = QRect(0, 0, 800, 600), QWidget *parent = nullptr);
    ~ScreenshotPanel();

protected:
    virtual void paintEvent(QPaintEvent *event) override;
    virtual void mousePressEvent(QMouseEvent *event) override;
    virtual  void mouseReleaseEvent(QMouseEvent *event) override;
    virtual  void mouseMoveEvent(QMouseEvent *event) override;
protected:
    virtual void createToolBar();

   /**
   * @brief handleCommand
   * @param cmd
   * @return true 如果没有自定义处理完了，就有处理handleCommand
   */
    virtual   bool handleCustomeCommand(ToolButtonEx *btn) override;
private:

    /**
     * @brief 开始选择区域
     * @param event
     */
    void startSelectArea(QMouseEvent *event) ;

    /**
     * @brief 移动选择区域
     * @param event
     */
    void moveSelectedArea(QMouseEvent *event) ;

    /**
     * @brief 选择区域完成
     * @param event
     */
    void selectAreaFinished(QMouseEvent *event) ;

    /**
     * @brief 绘制操作点
     * @param painter
     */
    void drawHandler(QPainter &painter);

    /**
     * @brief 移动选区时，如果超出了窗口的边界，就调整选区
     * 确保选区不超出窗口边界
     */
    void adjustSelection();

    /**
     * @brief 选区位置变化时，根据选区更新操作点的位置
     * 始终保持在选区的边框上。
     */
    void updateHandlerPosition();

    /**
     * @brief 更新选区模式下的鼠标图形
     */
    void updateSelectCursor();

    /**
     * @brief 调整选区的大小
     * @param offset
     */
    void resizeSelection(const QPoint & offset);


    /**
     * @brief 调整工具栏的位置
     */
    void updateToolBarPosition();


private:
    int m_opacity = 100.0f; ///< 0，表示完全透明，255表示完全不透明，
                            ///< 这里设置中间值，实现半透明

    QColor m_uiColor;   ///< 边框和操作点的颜色
    QPoint m_startPoint= QPoint(0,0); ///< 拖动或调整尺寸时的起点
    QVector<QRect > m_handles;    ///< 操作点的位置
    QRect m_selectionBeforeChange; ///< 变换前的选区
    int  m_resizeIndex = -1;
    QPoint m_offset = QPoint(0,0); ///< 移动的位置偏移
    bool m_selectNew = false;   ///<设置新选区
};
#endif // SCREENSHOTWIDGET_H
